package net.yla.smartbus.module.ba.right.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.vjtools.vjkit.collection.ListUtil;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.core.util.DtoUtils;
import net.yla.smartbus.core.util.IPUtil;
import net.yla.smartbus.core.util.qqwry.ext.QQWry;
import net.yla.smartbus.module.ba.right.admin.query.LogCount;
import net.yla.smartbus.module.ba.right.admin.query.LogQuery;
import net.yla.smartbus.module.ba.right.admin.query.SaveQuery;
import nl.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import net.yla.smartbus.module.ba.right.admin.mapper.AdminLoginLogsMapper;
import net.yla.smartbus.module.ba.right.admin.entity.AdminLoginLogsDO;
import net.yla.smartbus.module.ba.right.admin.service.AdminLoginLogsService;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2020-08-18 10:13
 * @Author: Being Nobody
 * @Version: 1.0
 */
@Service
public class AdminLoginLogsServiceImpl implements AdminLoginLogsService {
    @Autowired
    QQWry qqWry;
    @Resource
    private AdminLoginLogsMapper adminLoginLogsMapper;

    @Override
    public int deleteByPrimaryKey(Integer logId) {
        return adminLoginLogsMapper.deleteByPrimaryKey(logId);
    }

    @Override
    public int insert(AdminLoginLogsDO record) {
        return adminLoginLogsMapper.insert(record);
    }

    @Override
    public int insertSelective(AdminLoginLogsDO record) {
        return adminLoginLogsMapper.insertSelective(record);
    }

    @Override
    public AdminLoginLogsDO selectByPrimaryKey(Integer logId) {
        return adminLoginLogsMapper.selectByPrimaryKey(logId);
    }

    @Override
    public int updateByPrimaryKeySelective(AdminLoginLogsDO record) {
        return adminLoginLogsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AdminLoginLogsDO record) {
        return adminLoginLogsMapper.updateByPrimaryKey(record);
    }

    @Override
    public int logOfCount(LogCount query, long ip) {
        return adminLoginLogsMapper.logOfCount(query, ip);
    }

    @Override
    public PageBean<JSONObject> logOfQuery(LogQuery query, Long ip) {
        PageHelper.startPage(query.getPage(), query.getSize(), "creationTime"+" "+"desc");
        List<AdminLoginLogsDO> objectList = adminLoginLogsMapper.logOfQuery(query, ip);
        List<JSONObject> jsonObjects = this.disposeLog(objectList);
        PageInfo pageInfo = new PageInfo(objectList);
        if (ListUtil.isEmpty(jsonObjects)) {
            return new PageBean<JSONObject>(ListUtil.emptyList(), query.getPage(), query.getSize(), 0);
        }
        return new PageBean<>(jsonObjects, query.getPage(), query.getSize(), (int) pageInfo.getTotal());
    }

    /**
     * 查看管理员登录日志记录
     *
     * @param logId
     * @return
     */
    @Override
    public JSONObject logOfView(Integer logId) {
        AdminLoginLogsDO logsDO = adminLoginLogsMapper.selectByPrimaryKey(logId);
        JSONObject jsonObject = null;
        if (logsDO != null) {
            jsonObject = new JSONObject();
            jsonObject.put("id", logsDO.getLogId());
            jsonObject.put("admin", logsDO.getAdminId());
            jsonObject.put("ip", IPUtil.longToIpv4(logsDO.getLoginIP()));
            jsonObject.put("state", logsDO.getLoginState());
            jsonObject.put("url", logsDO.getUrl());
            jsonObject.put("referrer", logsDO.getReferrer());
            jsonObject.put("form", logsDO.getForm());
            jsonObject.put("headers", logsDO.getHeaders());
        }
        return jsonObject;
    }
    /**
     * 处理返回结果集
     *
     * @param objectList
     * @return
     */
    private List<JSONObject> disposeLog(List<AdminLoginLogsDO> objectList) {
        List<JSONObject> result = null;
        if (ListUtil.isNotEmpty(objectList) && objectList.size() > 0) {
            result = new ArrayList<>(objectList.size());
            for (AdminLoginLogsDO logsDO : objectList) {
                JSONObject object = new JSONObject();
                object.put("id", logsDO.getLogId());
                object.put("admin", logsDO.getAdminId());
                object.put("ip", IPUtil.longToIpv4(logsDO.getLoginIP()));
                object.put("state", logsDO.getLoginState());
                object.put("url", logsDO.getReferrer());
                object.put("referrer", logsDO.getUrl());
                object.put("form", logsDO.getLogId());
                object.put("headers", logsDO.getHeaders());
                if (logsDO != null) {
                    UserAgent userAgent = UserAgent.parseUserAgentString(logsDO.getHeaders());
                    object.put("browser", userAgent.getBrowser().toString().substring(0, 1).toUpperCase() + userAgent.getBrowser().toString().substring(1).toLowerCase());
                    object.put("system", userAgent.getOperatingSystem().toString().substring(0, 1).toUpperCase() + userAgent.getOperatingSystem().toString().substring(1).toLowerCase());
                } else {
                    object.put("browser", "- - -");
                    object.put("system", "- - -");
                }
                object.put("loginId", logsDO.getLoginId());
                object.put("time", logsDO.getCreationTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                object.put("loginIP", qqWry.findIP(IPUtil.longToIpv4(logsDO.getLoginIP())).getMainInfo());
                result.add(object);
            }
        }
        return result;
    }

}
